<?php defined('SYSPATH') or die('No direct script access.');

//-- Environment setup --------------------------------------------------------

/**
 * Set the default time zone.
 *
 * @see  http://kohanaframework.org/guide/using.configuration
 * @see  http://php.net/timezones
 */
date_default_timezone_set('Europe/Prague');

/**
 * Set the default locale.
 *
 * @see  http://kohanaframework.org/guide/using.configuration
 * @see  http://php.net/setlocale
 */
SetLocale(LC_ALL, "cs_CZ.UTF-8","Czech","CS");

/**
 * Enable the Kohana auto-loader.
 *
 * @see  http://kohanaframework.org/guide/using.autoloading
 * @see  http://php.net/spl_autoload_register
 */
spl_autoload_register(array('Kohana', 'auto_load'));

/**
 * Enable the Kohana auto-loader for unserialization.
 *
 * @see  http://php.net/spl_autoload_call
 * @see  http://php.net/manual/var.configuration.php#unserialize-callback-func
 */
ini_set('unserialize_callback_func', 'spl_autoload_call');

//-- Configuration and initialization -----------------------------------------

/**
 * Set Kohana::$environment if $_ENV['KOHANA_ENV'] has been supplied.
 * 
 */
if (isset($_ENV['KOHANA_ENV']))
{
	Kohana::$environment = $_ENV['KOHANA_ENV'];
}

/**
 * Initialize Kohana, setting the default options.
 *
 * The following options are available:
 *
 * - string   base_url    path, and optionally domain, of your application   NULL
 * - string   index_file  name of your index file, usually "index.php"       index.php
 * - string   charset     internal character set used for input and output   utf-8
 * - string   cache_dir   set the internal cache directory                   APPPATH/cache
 * - boolean  errors      enable or disable error handling                   TRUE
 * - boolean  profile     enable or disable internal profiling               TRUE
 * - boolean  caching     enable or disable internal caching                 FALSE
 */
Kohana::init(array(
	'base_url'   => 'http://photovoice/',
	'index_file' => FALSE,
  'profile'    => TRUE,
  'caching'    => TRUE
));

/**
 * Attach the file write to logging. Multiple writers are supported.
 */
Kohana::$log->attach(new Kohana_Log_File(APPPATH.'logs'));

/**
 * Attach a file reader to config. Multiple readers are supported.
 */
Kohana::$config->attach(new Kohana_Config_File);

/**
 * Enable modules. Modules are referenced by a relative or absolute path.
 */
Kohana::modules(array(
   'hitlogger'  => MODPATH.'hitlogger',  // Stats
   'user'       => MODPATH.'user',       // User authentication
   'auth'       => MODPATH.'auth',       // Basic authentication
   'cache'      => MODPATH.'cache',      // Caching with multiple backends
   'codebench'  => MODPATH.'codebench',  // Benchmarking tool
   'database'   => MODPATH.'database',   // Database access
   'image'      => MODPATH.'image',      // Image manipulation
   'orm'        => MODPATH.'orm',        // Object Relationship Mapping
  // 'oauth'      => MODPATH.'oauth',      // OAuth authentication
   'pagination' => MODPATH.'pagination', // Paging of results
  // 'unittest'   => MODPATH.'unittest',   // Unit testing
   'userguide'  => MODPATH.'userguide',  // User guide and API documentation
  ));

/**
 * Set the routes. Each route must have a minimum of a name, a URI and a set of
 * defaults for the URI.
 */
Route::set('messages/delete', 'messages/delete(/<url>(/<id>))')
        ->defaults(array(
            'controller' => 'messages',
            'action' => 'delete',
        ));

Route::set('article', 'article(/<page>)', array('page' => '[a-zA-Z0-9-]+'))
        ->defaults(array(
            'controller' => 'articles',
            'action' => 'view',
        ));

Route::set('discussion', 'discussion(/<page>)', array('page' => '[a-zA-Z0-9-]+'))
        ->defaults(array(
            'controller' => 'discussions',
            'action' => 'view',
        ));

Route::set('discussions', 'discussions(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'discussions',
            'action' => 'index',
        ));

Route::set('blog', 'blog(/<page>)', array('page' => '[a-zA-Z0-9-]+'))
        ->defaults(array(
            'controller' => 'blogs',
            'action' => 'view',
        ));

Route::set('gallery', 'gallery(/<page>)', array('page' => '[0-9]+'))
        ->defaults(array(
            'controller' => 'gallery',
            'action' => 'index',
        ));

Route::set('theme', 'theme(/<name>)', array('name' => '[a-z]+'))
        ->defaults(array(
            'controller' => 'theme',
            'action' => 'index',
        ));

Route::set('users', 'users(/<page>)', array('page' => '[0-9]+'))
        ->defaults(array(
            'controller' => 'users',
            'action' => 'index',
        ));

Route::set('administration', 'administration(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'administration',
            'action' => 'index',
        ));

Route::set('ajax', 'ajax(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'ajax',
            'action' => 'index',
        ));

Route::set('blogs', 'blogs(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'blogs',
            'action' => 'index',
        ));

Route::set('comments', 'comments(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'comments',
            'action' => 'index',
        ));

Route::set('files', 'files(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'files',
            'action' => 'index',
        ));

Route::set('messages', 'messages(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'messages',
            'action' => 'index',
        ));

Route::set('notes', 'notes(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'notes',
            'action' => 'index',
        ));

Route::set('offline', 'offline(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'offline',
            'action' => 'index',
        ));

Route::set('photo', 'photo(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'photo',
            'action' => 'index',
        ));

Route::set('request', 'request(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'request',
            'action' => 'index',
        ));

Route::set('stats', 'stats(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'stats',
            'action' => 'index',
        ));

Route::set('articles', 'articles(/<action>(/<id>))')
        ->defaults(array(
            'controller' => 'articles',
            'action' => 'index',
        ));

Route::set('<username>', '<username>(<controller>(/<action>(/<id>)))')
        ->defaults(array(
            'controller' => 'profile',
            'action' => 'index',
        ));

Route::set('default', '(<controller>(/<action>(/<id>)))')
        ->defaults(array(
            'controller' => 'index',
            'action' => 'index',
        ));

  // Define the approved languages
  $languages = array('en-us', 'cs-cz');

  // If language not set in _Session set the default
  if( ! isset($_SESSION['lang']) ) {
      $_SESSION['lang'] = 'cs-cz';
  }

  // Look for a requested change in language
  if( isset($_GET['lang']) AND in_array($_GET['lang'], $languages) ) {
      $_SESSION['lang'] = $_GET['lang'];
  }

  i18n::lang($_SESSION['lang']);


if ( ! defined('SUPPRESS_REQUEST'))
{
	/**
	 * Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
	 * If no source is specified, the URI will be automatically detected.
	 */
	echo Request::instance()
		->execute()
		->send_headers()
		->response;
}
